package yhao.infra.feature.aliyun.oss.demo;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import yhao.infra.feature.aliyun.oss.core.AliyunOssComponent;
import yhao.infra.feature.aliyun.oss.core.request.AliyunOssUploadRequest;
import yhao.infra.feature.aliyun.oss.core.response.AliyunOssUploadResponse;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * @author yoara
 * <p> Created on 2019/9/16 </p>
 */
@Component
public class AliyunOssDemo {
    @Resource
    private AliyunOssComponent aliyunOssComponent;

    public AliyunOssUploadResponse upload(MultipartFile file) throws Exception{
        AliyunOssUploadRequest request = new AliyunOssUploadRequest(file);
        return request.doRequest(aliyunOssComponent);
    }

    public void download(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String url = request.getParameter("url");
        String fileName = request.getParameter("fileName");
        if(StringUtils.isBlank(fileName)){
            fileName = url.substring(url.lastIndexOf("/")+1);
        }
        aliyunOssComponent.download(request,response,url,fileName);
    }
}
